package com.hubt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hubt.common.PageBean;
import com.hubt.controller.PtRUserDutyController;
import com.hubt.mapper.PtOrganMapper;
import com.hubt.mapper.PtRRoleOrganMapper;
import com.hubt.mapper.PtRUserDutyOrgMapper;
import com.hubt.mapper.PtRoleMapper;
import com.hubt.mapper.PtUserMapper;
import com.hubt.model.PtOrgan;
import com.hubt.model.PtRRoleOrgan;
import com.hubt.model.PtRUserDutyOrg;
import com.hubt.model.PtRole;
import com.hubt.service.PtRDutyService;
import com.hubt.vo.Dutyvo;
import com.hubt.vo.Organvo;
import com.hubt.vo.RoleVo;

@Service("ptRDutyService")
@Transactional
public class PtRDutyServiceImpl implements PtRDutyService {

	@Autowired
	PtRRoleOrganMapper ptRRoleOrganmapper;
	
	@Autowired
	PtOrganMapper ptorganmapper;
	
	@Autowired
	PtRoleMapper ptrolemapper;
	
	@Autowired
	PtRUserDutyOrgMapper ptDutyMapper;
	
	@Override
	public PageBean<Dutyvo> findBypage(int pageNumber, int pageSize) {
		// TODO Auto-generated method stub
		
		List<PtRRoleOrgan> list=ptRRoleOrganmapper.findAllByPage((pageNumber-1)*pageSize, pageSize);
		List<Dutyvo> dutyList=new ArrayList<Dutyvo>();
		
		for(PtRRoleOrgan t:list) {
			Dutyvo dutyvo=new Dutyvo();
			dutyvo.setDutyId(t.getDutyid());
			dutyvo.setName(t.getName());
			if(ptorganmapper.selectByPrimaryKey(t.getOrganUuid())==null) {
				dutyvo.setOrgan(null);
			}else {
				PtOrgan organ=ptorganmapper.selectByPrimaryKey(t.getOrganUuid());
				Organvo t1=new Organvo();
				t1.setOrgan_uuid(organ.getOrganUuid());
				t1.setOrgan_code(organ.getOrganCode());
				t1.setOrgan_name(organ.getOrganName());
				t1.setModtime(organ.getModtime());
				t1.setOrgan_type(organ.getOrganType());
				t1.setShortname(organ.getShortname());
				t1.setIn_use(organ.getInUse());
				t1.setParent_uuid(organ.getParentUuid());
				dutyvo.setOrgan(t1);
			}
			if(ptrolemapper.selectByPrimaryKey(t.getRoleUuid())==null) {
				dutyvo.setRole(null);
			}else {
				PtRole role=ptrolemapper.selectByPrimaryKey(t.getRoleUuid());
				RoleVo t2=new RoleVo();
				t2.setRole_id(role.getRoleId());
				t2.setRole_name(role.getRoleName());
				t2.setRole_uuid(role.getRoleUuid());
				dutyvo.setRole(t2);
			}
			
			dutyList.add(dutyvo);
		}
		PageBean<Dutyvo> pageBean=new PageBean<Dutyvo>();
		pageBean.setRows(dutyList);
		int total=ptRRoleOrganmapper.countSize();
		pageBean.setTotal(total);
		pageBean.setPageNumber(pageNumber);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((total%pageNumber==0)?(total/pageNumber):(total/pageSize+1));
		return pageBean;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
//		try {
			if(ptDutyMapper.findByduty(id)==null) {
				ptRRoleOrganmapper.deleteByPrimaryKey(id);
			}else {
				ptDutyMapper.delete(id);
				ptRRoleOrganmapper.deleteByPrimaryKey(id);
			}			
			return 1;
//		} catch (Exception e) {
//			// TODO: handle exception
//			return 0;
//		}
		
	}

	@Override
	public int insert(Dutyvo dutyvo) {
		// TODO Auto-generated method stub
		try {
			PtRRoleOrgan organ=new PtRRoleOrgan();
			
			organ.setDutyid(dutyvo.getRole().getRole_uuid()+(int)(1+Math.random()*10));
			organ.setName(dutyvo.getName());
			organ.setOrganUuid(dutyvo.getOrgan().getOrgan_uuid());
			organ.setRoleUuid(dutyvo.getRole().getRole_uuid());
			ptRRoleOrganmapper.insert(organ);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}	
	}

	@Override
	public int update(Dutyvo dutyvo) {
		// TODO Auto-generated method stub
		try {
PtRRoleOrgan organ=new PtRRoleOrgan();
			
			organ.setDutyid(dutyvo.getDutyId());
			organ.setName(dutyvo.getName());
			organ.setOrganUuid(dutyvo.getOrgan().getOrgan_uuid());
			organ.setRoleUuid(dutyvo.getRole().getRole_uuid());
			ptRRoleOrganmapper.updateByPrimaryKeySelective(organ);
			return 1;
		} catch (Exception e) {
			
			// TODO: handle exception
			return 0;
		}
		
	}

	@Override
	public List<Dutyvo> getbyOrg(String id) {
		// TODO Auto-generated method stubLi
		List<Dutyvo> list=new ArrayList<Dutyvo>();
		List<PtRRoleOrgan> list2=ptRRoleOrganmapper.findByduty(id);
		for(PtRRoleOrgan t:list2) {
			Dutyvo dutyvo=new Dutyvo();
			dutyvo.setDutyId(t.getDutyid());
			dutyvo.setName(t.getName());
			if(ptorganmapper.selectByPrimaryKey(t.getOrganUuid())==null) {
				dutyvo.setOrgan(null);
			}else {
				PtOrgan organ=ptorganmapper.selectByPrimaryKey(t.getOrganUuid());
				Organvo organvo = new Organvo();
				organvo.setOrgan_uuid(organ.getOrganUuid());
				organvo.setOrgan_code(organ.getOrganCode());
				organvo.setOrgan_name(organ.getOrganName());
				organvo.setModtime(organ.getModtime());
				organvo.setOrgan_type(organ.getOrganType());
				organvo.setShortname(organ.getShortname());
				organvo.setIn_use(organ.getInUse());
				organvo.setParent_uuid(organ.getParentUuid());
				dutyvo.setOrgan(organvo);
				
			}
			if(ptrolemapper.selectByPrimaryKey(t.getRoleUuid())==null) {
				dutyvo.setRole(null);
			}else {
				PtRole ptRole=ptrolemapper.selectByPrimaryKey(t.getRoleUuid());
				RoleVo roleVo=new RoleVo();
				roleVo.setRole_id(ptRole.getRoleId());
				roleVo.setRole_name(ptRole.getRoleName());
				roleVo.setRole_uuid(ptRole.getRoleUuid());
				dutyvo.setRole(roleVo);
			}
			list.add(dutyvo);
		}
		return list;
	}

	@Override
	public Dutyvo getOne(String id) {
		// TODO Auto-generated method stub
		Dutyvo dutyvo=new Dutyvo();
		PtRRoleOrgan ptrorgan=ptRRoleOrganmapper.selectByPrimaryKey(id);
		dutyvo.setDutyId(ptrorgan.getDutyid());
		dutyvo.setName(ptrorgan.getName());
		if(ptorganmapper.selectByPrimaryKey(ptrorgan.getOrganUuid())==null) {
			dutyvo.setOrgan(null);
		}else {
			PtOrgan organ=ptorganmapper.selectByPrimaryKey(ptrorgan.getOrganUuid());
			Organvo organvo = new Organvo();
			organvo.setOrgan_uuid(organ.getOrganUuid());
			organvo.setOrgan_code(organ.getOrganCode());
			organvo.setOrgan_name(organ.getOrganName());
			organvo.setModtime(organ.getModtime());
			organvo.setOrgan_type(organ.getOrganType());
			organvo.setShortname(organ.getShortname());
			organvo.setIn_use(organ.getInUse());
			organvo.setParent_uuid(organ.getParentUuid());
			dutyvo.setOrgan(organvo);
			
		}
		if(ptrolemapper.selectByPrimaryKey(ptrorgan.getRoleUuid())==null) {
			dutyvo.setRole(null);
		}else {
			PtRole ptRole=ptrolemapper.selectByPrimaryKey(ptrorgan.getRoleUuid());
			RoleVo roleVo=new RoleVo();
			roleVo.setRole_id(ptRole.getRoleId());
			roleVo.setRole_name(ptRole.getRoleName());
			roleVo.setRole_uuid(ptRole.getRoleUuid());
			dutyvo.setRole(roleVo);
		}
		
		return dutyvo;
	}

}
